//Edge detection with threshold slider
//Run in Release --> Debug app will crash w/ mouse over slider

#include "testApp.h"



//--------------------------------------------------------------
void testApp::setup(){
    ofSetLogLevel(OF_LOG_VERBOSE);

    clrImg.allocate(320,240,OF_IMAGE_COLOR);
    cvClrImg.allocate(clrImg.getWidth(),clrImg.getHeight());
    cvGrayImg.allocate(cvClrImg.getWidth(),cvClrImg.getHeight());

    cannyImg.allocate(cvGrayImg.getWidth(),cvGrayImg.getHeight());

    //load jpeg into ofImage
    clrImg.loadImage("perspective-quadrilateral-src-img.jpg");

    //load pixels from jpeg into array
    unsigned char * pixels = clrImg.getPixels();

    //load pixels from array into ofxCvColorImage
    cvClrImg.setFromPixels(pixels, 320,240);

    //convert ofxCvColourImage to ofxCvGrayscaleImage
    cvGrayImg = cvClrImg;

    //Gui panel setup
    gui.setup("Threshold Slider");
    gui.setPosition(700,20);
    //add threshold min and max sliders
    gui.add(threshMin.setup( "Min", 150, 0, 500 ));
    gui.add(threshMax.setup( "Max", 250, 0, 500 ));

}

//--------------------------------------------------------------
void testApp::update(){

    //get the edge map
    cvCanny(cvGrayImg.getCvImage(), cannyImg.getCvImage(), threshMax,  threshMin, 3);
    //update cannyImg
    cannyImg.flagImageChanged();


}

//--------------------------------------------------------------
void testApp::draw(){
    ofBackground(100, 100, 100);

    //draw original colour image
    cvClrImg.draw(20, 20, 320, 240);

    //draw grayscale image
    cvGrayImg.draw(20, 280, 320, 240);

    //draw edge map
    cannyImg.draw(360,20,320,240);

    //draw gui panel
    gui.draw();



}


















//--------------------------------------------------------------
void testApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}
